package com.eagle.videoboot.utils;

import sun.net.www.content.image.gif;

import java.io.*;
import java.util.List;

/**
 * 
 * @Description: 获取视频的信息
 */
public class FetchVideoCover {

	public void getCover(String videoInputPath, String coverOutputPath) throws IOException, InterruptedException {
//		ffmpeg.exe -ss 00:00:01 -i spring.mp4 -vframes 1 bb.jpg

		List<String> command = new java.util.ArrayList<String>();
		command.add("ffmpeg");
		
		// 指定截取第1秒
		command.add("-ss");
		command.add("00:00:01");
				
		command.add("-y");
		command.add("-i");
		command.add(videoInputPath);
		
		command.add("-vframes");
		command.add("1");
		
		command.add(coverOutputPath);
		
		for (String c : command) {
			System.out.print(c + " ");
		}
		
		ProcessBuilder builder = new ProcessBuilder(command);
		Process process = builder.start();
		
		InputStream errorStream = process.getErrorStream();
		InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
		BufferedReader br = new BufferedReader(inputStreamReader);
		
		String line = "";
		while ( (line = br.readLine()) != null ) {
		}
		
		if (br != null) {
			br.close();
		}
		if (inputStreamReader != null) {
			inputStreamReader.close();
		}
		if (errorStream != null) {
			errorStream.close();
		}

//		File file = new File(videoInputPath);
//		if (file.exists() && file.isFile()){
//			file.delete();
//		}
	}

	
	public static void main(String[] args) {
		// 获取视频信息。
		FetchVideoCover videoInfo = new FetchVideoCover();
		try {
			videoInfo.getCover("F:\\video-upload\\200423H3XG9AC1KP\\video\\4238f0f8-baab-470d-8402-3f564ade82bc.mp4",
					"F:\\video-upload\\200423H3XG9AC1KP\\video\\4238f0f8-baab-470d-8402-3f564ade82bc.jpg");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}